package com.example.main;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import com.example.main.beans.BarcodeInfoBean;
import com.example.main.utils.ViewSetUtils;

import java.util.ArrayList;
import java.util.List;

public class SaveViewActivity extends Activity {

    private static final String TAG = "SaveViewActivity";

    private Button btn_save;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_save_view);
        btn_save = findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<BarcodeInfoBean> barcodeInfoBeanList = new ArrayList<>();
                barcodeInfoBeanList.add(new BarcodeInfoBean("1", "内容1"));
                barcodeInfoBeanList.add(new BarcodeInfoBean("222", "内容1"));
                barcodeInfoBeanList.add(new BarcodeInfoBean("333", "内容2"));
                barcodeInfoBeanList.add(new BarcodeInfoBean("444", "内容3"));
                barcodeInfoBeanList.add(new BarcodeInfoBean("555", "内容4"));
                exportBarImage(700, 400, barcodeInfoBeanList);
            }
        });
    }

    /**
     * 导出图片
     *
     * @param imageWidth
     * @param itemHeight
     * @param barcodeInfoBeanList
     */
    private void exportBarImage(int imageWidth, int itemHeight, List<BarcodeInfoBean> barcodeInfoBeanList) {
        SaveView saveView = new SaveView(SaveViewActivity.this, imageWidth, itemHeight, barcodeInfoBeanList);
        ViewSetUtils.layoutView(saveView, imageWidth, itemHeight * barcodeInfoBeanList.size());
        Bitmap cacheBitmapFromView = ViewSetUtils.getCacheBitmapFromView(saveView);
        ViewSetUtils.saveBitmapToSdCard(SaveViewActivity.this, cacheBitmapFromView, "barImage");

        for (int i = 0; i < barcodeInfoBeanList.size(); i++) {
            if (!barcodeInfoBeanList.get(i).getBitmap().isRecycled()) {
                barcodeInfoBeanList.get(i).getBitmap().recycle();
            }
        }
        if (!cacheBitmapFromView.isRecycled()) {
            cacheBitmapFromView.recycle();
        }

        System.gc();
    }
}
